﻿#ifndef __DEFAULT_CONVERTOR_H__
#define __DEFAULT_CONVERTOR_H__

#include "./luaobj.h"

//getvalue
template <typename T>
inline T getvalue(lua_State* luaState, int i)
{
	return *(T*)luaL_checkudata(luaState, i, Wrapper<T>::GetObjectType());
}
template <>
inline const char * getvalue<const char *>(lua_State* luaState, int i)
{
    return luaL_checkstring(luaState, i);
}
template <>
inline lua_Integer getvalue<lua_Integer>(lua_State* luaState, int i)
{
    return luaL_checkinteger(luaState, i);
}
template <>
inline unsigned int getvalue<unsigned int>(lua_State* luaState, int i)
{
    return (unsigned int)luaL_checkinteger(luaState, i);
}
template <>
inline char getvalue<char>(lua_State* luaState, int i)
{
    return (char)luaL_checkinteger(luaState, i);
}
template <>
inline unsigned char getvalue<unsigned char>(lua_State* luaState, int i)
{
    return (unsigned char)luaL_checkinteger(luaState, i);
}
template <>
inline bool getvalue<bool>(lua_State* luaState, int i)
{
    return lua_toboolean(luaState, i) == 1;
}
template <>
inline float getvalue<float>(lua_State* luaState, int i)
{
    return (float)luaL_checknumber(luaState, i);
}
template <>
inline lua_Number getvalue<lua_Number>(lua_State* luaState, int i)
{
    return lua_tonumber(luaState, i);
}

//setvalue
template <class T>
inline void setvalue(lua_State* luaState, T& val)
{
	//non recognize type
}

template <>
inline void setvalue<luaobj>(lua_State* luaState, luaobj& obj)
{
    obj->PushObj(luaState);
}

template <>
inline void setvalue<const char *>(lua_State* luaState, const char *& val)
{
    lua_pushstring(luaState, val);
}
template <>
inline void setvalue<char>(lua_State* luaState, char& val)
{
    lua_pushinteger(luaState, val);
}
template <>
inline void setvalue<unsigned char>(lua_State* luaState, unsigned char& val)
{
    lua_pushinteger(luaState, val);
}
template <>
inline void setvalue<unsigned int>(lua_State* luaState, unsigned int& val)
{
    lua_pushinteger(luaState, val);
}
template <>
inline void setvalue<lua_Integer>(lua_State* luaState, lua_Integer& val)
{
    lua_pushinteger(luaState, val);
}
template <>
inline void setvalue<bool>(lua_State* luaState, bool& val)
{
    lua_pushboolean(luaState, val);
}
template <>
inline void setvalue<lua_Number>(lua_State* luaState, lua_Number& val)
{
    lua_pushnumber(luaState, val);
}
template <>
inline void setvalue<float>(lua_State* luaState, float& val)
{
    lua_pushnumber(luaState, val);
}

#endif